$(function () {
	$(window).resize(function(){
		$("#jqGrid").setGridWidth($(window).width());
	});
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/keywordinfo/list',
        datatype: "json",
        colModel: [
			{ label: '关键字内容', name: 'id', index: 'id',align: "center", key: true ,width:120,formatter:function(value,options,rowData){
					var keywordContent=rowData["keywordContent"];
					if(1==1){
						return "<a  onclick='update("+value+")'>"+keywordContent+"</a>";
					}
				}
			},
			{ label: '内容类型', name: 'keywordContentType', index: 'keyword_content_type',align: "center",width:120,
				formatter:function(value,options,rowData){
				if(vm.q.type==1 && value==0){
					return '无';
				}
				if( value==0 ){
						return '不分左右';
					}else if(value==1){
						return '左';
					}else if(value==2){
						return '右';
					}else if(value==3){
						return '正则';
				}
				}
			},
			{ label: '创建时间', name: 'createTime', index: 'create_time',align: "center",width:120},
			{ label: '修改时间', name: 'modifyTime', index: 'modify_time',align: "center",width:120},
			{ label: '状态', name: 'keywordStatus', index: 'keyword_status',align: "center",width:120,formatter:function(value,options,rowData){
				if (value==1){
					return "正常";
				}else {
					return "<p style='color: red'>"+'删除'+"</>"
				}
				}
			},
			{ label: '操作', name: 'keywordStatus', index: 'keyword_status',align: "center",formatter:function(value,options,rowData){
					var id=rowData["id"];
					if(value==1) {
						return "<a  style='text-align: center' onclick=status('"+id+"',0)>" + '删除' + "</a>";
					}else{
						return "<a  style='text-align: center' onclick=status('"+id+"',1)>" + '恢复' + "</a>";
					}
				}
			},
        ],
		viewrecords: true,
		height: '100%',
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true,
		autowidth: true,
        rownumWidth: 25,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
		postData:{"keywordType":vm.q.type,"keywordContent":vm.q.keywordContent},
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
	$(window).resize(function(){
		$("#jqGrid").setGridWidth($(window).width());
	});
});
function update(e) {
	vm.showList = false;
	vm.title = "修改";
	vm.getInfo(e)
}
function status(e,x) {
	vm.keywordInfo.id=e;
	vm.keywordInfo.keywordStatus=x;
	confirm('确定要删除选中的记录？', function(){
		$.ajax({
			type: "POST",
			url: baseURL + "sys/keywordinfo/update",
			contentType: "application/json",
			data: JSON.stringify(vm.keywordInfo),
			success: function(r){
				if(r.code == 0){
					alert('操作成功', function(index){
						$("#jqGrid").trigger("reloadGrid");
					});
				}else{
					alert(r.msg);
				}
			}
		});
	});
}
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		keywordInfo: {},
		q:{
			type:"",
			keywordContent:""
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.keywordInfo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			vm.keywordInfo.keywordType=vm.q.type;
			// if(vm.keywordInfo.keywordType==null || vm.keywordInfo.keywordType==""){
			// 	vm.keywordInfo.keywordType=0;
			// }
			if($('#checked').prop('checked')){
				vm.keywordInfo.keywordContentType=3;
				console.log("广告");
			}else{
				console.log("其他"+$("#contentType").val());
				vm.keywordInfo.keywordContentType=$("#contentType").val();
				if(vm.keywordInfo.keywordContent==null||vm.keywordInfo.keywordContent==""){
					alert("请填写完整信息");
					return;
				}
			}
			var url = vm.keywordInfo.id == null ? "sys/keywordinfo/save" : "sys/keywordinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.keywordInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/keywordinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/keywordinfo/info/"+id, function(r){
                vm.keywordInfo = r.keywordInfo;
                console.log(vm.keywordInfo.keywordType+">>>"+vm.keywordInfo.keywordContentType)
				$("#wordType").find("option[value='" + vm.keywordInfo.keywordType + "']").attr("selected", true);
				$("#contentType").find("option[value='" + vm.keywordInfo.keywordContentType + "']").attr("selected", true);
				if(vm.keywordInfo.keywordContentType==3){
					$("#checked").attr("checked","checked");
				}
				if(vm.keywordInfo.keywordType==1){
					vm.q.type=1;
				}else if(vm.keywordInfo.keywordType==2){
					vm.q.type=2;
				}else if(vm.keywordInfo.keywordType==0){
					vm.q.type=0;
				}
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.q.type=$("#type").val();
			var page =  $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
				postData:{"keywordType":vm.q.type,"keywordContent":vm.q.keywordContent},
            }).trigger("reloadGrid");
		}
	}
});